package com.thinkin.ordercenter.buz;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkin.ordercenter.dao.OonlineShopDao;
import com.thinkin.ordercenter.dao.impl.OonlineShopDaoImpl;
import com.thinkin.ordercenter.po.OonlineShop;
import com.thinkin.ordercenter.vo.OonlineShopVo;

public class OonlineShopBuz {
	Logger logger = LoggerFactory.getLogger(OonlineShopBuz.class);

	private OonlineShopDao oonlineShopDao = null;

	public OonlineShopBuz() {
		oonlineShopDao = new OonlineShopDaoImpl();
	}

	public void add(OonlineShop vo) {
		String sql = "INSERT  INTO O_ONLINESHOP(" + "KEYID,CHANNELKEYID,"
				+ "PLATFORMID,NETSHOPTYPE," + "NETSHOPUSERNAME,NETSHOPNAME,"
				+ "NETSHOPURL,FILEQUEUE," + "APPKEY,APPSECRET,"
				+ "APPSESSION,REFRESHTOKEN," + "CREATENAME,CREATEDATE"
				+ ") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		Object param[] = { vo.getKeyid(), vo.getChannelKeyid(),
				vo.getPlatformID(), vo.getNetshopType(),
				vo.getNetShopUserName(), vo.getNetShopName(),
				vo.getNetShopUrl(), vo.getFileQueue(), vo.getAppKey(),
				vo.getAppSecret(), vo.getAppSession(), vo.getRefreshToken(),

				vo.getCreatename(), vo.getCreatedate() };
		oonlineShopDao.add(sql, param, true);
	}

	public void modify(OonlineShop vo) {
		String sql = "UPDATE O_ONLINESHOP SET "
				+ "CHANNELKEYID=?,PLATFORMID=?,"
				+ "NETSHOPTYPE=?,NETSHOPUSERNAME=?,"
				+ "NETSHOPNAME=?,NETSHOPURL=?," + "FILEQUEUE=?,APPKEY=?,"
				+ "APPSECRET=?,APPSESSION=?," + "REFRESHTOKEN=?,MODIFYNAME=?,"
				+ "MODIFYDATE=? where KEYID=?";
		Object param[] = { vo.getChannelKeyid(), vo.getPlatformID(),
				vo.getNetshopType(), vo.getNetShopUserName(),
				vo.getNetShopName(), vo.getNetShopUrl(), vo.getFileQueue(),
				vo.getAppKey(), vo.getAppSecret(), vo.getAppSession(),
				vo.getRefreshToken(), vo.getModifyname(), vo.getModifydate(),
				vo.getKeyid() };

		oonlineShopDao.modify(sql, param, true);

	}

	public void delete(String keyid) {
		String sql = "delete from O_ONLINESHOP where KEYID=?";
		Object param[] = { keyid };
		oonlineShopDao.delete(sql, param, true);
	}

	public List<OonlineShop> query(OonlineShopVo qvo) {
		/*
		 * String sql =
		 * "SELECT * FROM O_ONLINESHOP WHERE NETSHOPNAME=?,PLATFORMID=?,NETSHOPTYPE=?"
		 * ; Object param[] = { qvo.getNetShopName(), qvo.getPlatformID(),
		 * qvo.getNetshopType() };
		 */
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM O_ONLINESHOP WHERE  1=1 ");
		if (StringUtils.isNotBlank(qvo.getNetShopName())) {
			stringBuffer.append(" and NETSHOPNAME=" + qvo.getNetShopName());
		}

		if (StringUtils.isNotBlank(qvo.getPlatformID())) {
			stringBuffer.append(" and PLATFORMID=" + qvo.getPlatformID());
		}

		if (StringUtils.isNotBlank(qvo.getNetshopType())) {
			stringBuffer.append(" and NETSHOPTYPE=" + qvo.getNetshopType());
		}

		List<OonlineShop> oonlineShopLs = oonlineShopDao.findQuery(
				stringBuffer.toString(), true);
		return oonlineShopLs != null && oonlineShopLs.size() > 0 ? oonlineShopLs
				: null;
	}

	public OonlineShop loadEntity(String keyid) {
		String sql = "select * from O_ONLINESHOP where  KEYID=?";

		OonlineShop oonlineShop = oonlineShopDao.find(sql, keyid, true);

		return oonlineShop;
	}
}
